#!/usr/bin/env python

import os, sys, getopt

def calculate_average():
    given_folder = "output/user_behavior_of_3600/"
    user_behavior_files = sorted(os.listdir(given_folder))

    output_file = "output/user_behavior_of_3600_average"
    with open(output_file, "w") as f:
        for user_behavior in user_behavior_files:
            with open(given_folder+user_behavior) as user_file:
                summation = 0
                for line in user_file:
                    time_slot, counter = [int(x) for x in line.split()]
                    summation += counter

                max_value = 696
                average = float(summation) / max_value
                content = "%.2f\n" % average
                f.write(content)


def calculate_variance():
    given_folder = "output/user_behavior_of_3600/"
    user_behavior_files = sorted(os.listdir(given_folder))

    output_file = "output/user_behavior_of_3600_variance"
    with open(output_file, "w") as f:
        for user_behavior in user_behavior_files:
            with open(given_folder+user_behavior) as user_file:
                summation = 0.0
                for line in user_file:
                    time_slot, counter = [int(x) for x in line.split()]
                    summation += counter
                
                n = 696
                average = summation / n
            
            with open(given_folder+user_behavior) as user_file:
                variance = 0.0
                for line in user_file:
                    time_slot, counter = [int(x) for x in line.split()]
                    variance += (counter-average) ** 2
                variance = variance / n

            content = "%.2f\n" % variance
            f.write(content)


def calculate_standard_variance():
    given_folder = "output/user_behavior_of_3600/"
    user_behavior_files = sorted(os.listdir(given_folder))

    output_file = "output/user_behavior_of_3600_standard_variance"
    with open(output_file, "w") as f:
        for user_behavior in user_behavior_files:
            with open(given_folder+user_behavior) as user_file:
                summation = 0.0
                for line in user_file:
                    time_slot, counter = [int(x) for x in line.split()]
                    summation += counter
                
                n = 696
                average = summation / n
            
            with open(given_folder+user_behavior) as user_file:
                variance = 0.0
                for line in user_file:
                    time_slot, counter = [int(x) for x in line.split()]
                    variance += (counter-average) ** 2
                variance = variance / n

            standard_variance = variance ** 0.5
            content = "%.2f\n" % standard_variance
            f.write(content)


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "hb:e:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    # set default value
    begin_index = 0
    end_index = 931

    for o, s in opts:
        # help information
        if o == "-h":
            print "usge   -b <begin user index>"
            print "       -e <end user index>"
            return
        elif o == "-b":
            begin_index = int(s)
        elif o == "-e":
            end_index = int(s)

    #calculate_average()
    #calculate_variance()
    calculate_standard_variance()
    

if __name__ == "__main__":
    main(sys.argv)